<template>
  <el-container>
    <el-header>
      <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="请选择地址" :label-width="formLabelWidth">
              <el-cascader
                  v-model="form.cityid"
                  :options="cityoptions"
                  @change="handleChange"></el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="commitcity()">确 定</el-button>
        </div>
      </el-dialog>
      <el-card style="background: paleturquoise">
        结算页,
        <el-button type="text" @click="goback()" align="right">返回购物车修改</el-button>
        <el-button type="text" @click="goorder()" align="right">我的订单</el-button>
      </el-card>
    </el-header>
    <el-main>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="收货人">
          <el-input v-model="formInline.oname" placeholder="请输入收货人姓名"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="formInline.ophone" placeholder="请输入收货人手机号"></el-input>
        </el-form-item>
        <el-form-item label="收货地址">
          <el-select v-model="formInline.address" placeholder="请选择收货地址">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.label">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">新增收货地址</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="detail"
          style="width: 100%">
        <el-table-column
            prop="pic"
            label="图片"
            width="180">
          <template slot-scope="scope">
            <img :src="scope.row.pic" width="80px" height="80px" alt="暂无图片">
          </template>
        </el-table-column>
        <el-table-column
            prop="gname"
            label="商品名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="price"
            label="商品单价"
            width="180">
        </el-table-column>
        <el-table-column
            prop="gnumber"
            label="数量">
        </el-table-column>
        <el-table-column
            prop="subtotal"
            label="小计">
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-card style="background: paleturquoise">
        总价:<i style="color:red">{{cartsum}}</i>元
        <span>
          <el-button type="danger" @click="getdetafororder()" round>提交订单</el-button>
        </span>
      </el-card>
    </el-footer>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      name1:"",
      name2:"",
      name3:"",
      nvalue1:"",
      nvalue2:"",
      nvalue3:"",
      cartsum:0,
      detail:[],
      formInline: {
        uuid:"",
        oname:"",
        ophone:"",
        address:"",
        gsum:0,
        detail:[],
      },
      dialogFormVisible: false,
      form: {
        cityid:[],
      },
      formLabelWidth: '120px',
      cityoptions:[],
      options:[],
    }
  },
  methods:{
    getdetail(){
      this.detail = JSON.parse(localStorage.getItem("cartlist"));
      this.cartsum =localStorage.getItem("cartsum");
    },
    getdetafororder() {
      this.formInline.detail=this.detail;
      this.formInline.gsum=this.cartsum;
      this.axios.post("http://localhost:9999/order/postdetail",this.formInline).then((res)=>{
        if (res.data.result){
          this.$message.success("提交成功");
          localStorage.removeItem("cartlist")
          this.axios.post("http://localhost:9999/cart/delcartbyuid",this.detail).then((res)=>{
            if (res){
              console.log(res);
            }
          })
        }else {
          this.$message.error(res.data.msg);
        }
      });
    },
    onSubmit() {
      this.axios.get("http://localhost:9999/city/getcity").then((res)=>{
        if (res){
          console.log(res.data);
          this.cityoptions=res.data;
        }
      })
      this.dialogFormVisible=true;
    },
    goback(){
      this.$router.push({path:"/cart"});
    },
    goorder(){
      this.$router.push({path:"/order"});
    },
    handleChange() {
      console.log(this.form.cityid);
    },
    commitcity(){
      this.axios.post("http://localhost:9999/address/commitcity",this.form).then((res)=>{
        if (res.data.code=="200"){
          this.$message.success("添加地址成功!");
          this.dialogFormVisible= false;
          location.reload();
        }
      })
    }
  },
  created() {
    this.getdetail();
    this.axios.get("http://localhost:9999/order/postuuid").then((res)=>{
      this.formInline.uuid=res.data.data;
    })
    this.axios.get("http://localhost:9999/address/getaddress").then((res)=>{
      this.options=res.data;
      console.log(res.data);
    })
  }
}
</script>

<style scoped>

</style>